package com.dmall.leetcode;

/**
 * LeetCode：203-移除链表元素
 * 使用虚拟节点的方式移除链表元素
 * @author xiao1.wang@dmall.com
 * @date 2019-03-23 22:15:30
 */
public class Solution2 {
    public ListNode removeElements(ListNode head, int val) {

        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode prev = dummyHead;
        while(prev.next != null) {
            if (prev.next.val == val) {
                prev.next = prev.next.next;
            } else {
                prev = prev.next;
            }
        }

        return dummyHead.next;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 6, 3, 4, 5, 6};
        ListNode head = new ListNode(arr);
        System.out.println(head);

        ListNode listNode = new Solution2().removeElements(head, 6);
        System.out.println(listNode);
    }
}
